package org.jeecg.cn.edu.scau.vm.machineTime.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.cn.edu.scau.vm.machineTime.entity.VmUserSettings;
import org.jeecg.cn.edu.scau.vm.machineTime.service.IVmUserSettingsService;
import org.jeecg.cn.edu.scau.vm.machineTime.vo.UserSettingsVo;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Api(tags = "用户基础配额设置")
@RestController
@RequestMapping("/vm/vmUserSettings")
@Slf4j
public class VmUserSettingsController {

    @Resource
    private IVmUserSettingsService vmUserSettingsService;

    /**
     * 同步用户数据
     *
     * @return 返回同步成功
     */
    @ApiOperation(value = "用户基础配置-同步配额数据", notes = "同步配额数据")
    @GetMapping("/syncData")
    public Result<String> syncData() {
        return vmUserSettingsService.syncData();
    }
    
    /**
     * 用户基础配置分页查询
     *
     * @param vmUserSettings 查询实体
     * @param pageNo         分页页码
     * @param pageSize       分页大小
     * @param req            req对象
     * @return 分页对象
     */
    @ApiOperation(value = "用户基础配置-分页列表查询", notes = "用户基础配置-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<UserSettingsVo>> queryPageList(VmUserSettings vmUserSettings,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {
        QueryWrapper<VmUserSettings> queryWrapper = QueryGenerator.initQueryWrapper(vmUserSettings, req.getParameterMap());
        Page<VmUserSettings> page = new Page<>(pageNo, pageSize);
        IPage<UserSettingsVo> pageList = vmUserSettingsService.pageInfo(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param vmUserSettings 配置实体
     * @return 添加相关API
     */
    @AutoLog(value = "用户基础配置-添加")
    @ApiOperation(value = "用户基础配置-添加", notes = "用户基础配置-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody VmUserSettings vmUserSettings) {
        vmUserSettingsService.save(vmUserSettings);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param vmUserSettings 编辑实体
     * @return 返回相关数据
     */
    @AutoLog(value = "用户基础配置-编辑")
    @ApiOperation(value = "用户基础配置-编辑", notes = "用户基础配置-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody VmUserSettings vmUserSettings) {
        vmUserSettingsService.updateById(vmUserSettings);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id 配置ID
     * @return 返回相关数据
     */
    @AutoLog(value = "用户基础配置-通过id删除")
    @ApiOperation(value = "用户基础配置-通过id删除", notes = "用户基础配置-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        vmUserSettingsService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids 数据集合列表
     * @return 返回提示语
     */
    @AutoLog(value = "用户基础配置-批量删除")
    @ApiOperation(value = "用户基础配置-批量删除", notes = "用户基础配置-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.vmUserSettingsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id 配置ID
     * @return 返回具体数据
     */
    //@AutoLog(value = "用户基础配置-通过id查询")
    @ApiOperation(value = "用户基础配置-通过id查询", notes = "用户基础配置-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<VmUserSettings> queryById(@RequestParam(name = "id", required = true) String id) {
        VmUserSettings vmUserSettings = vmUserSettingsService.getById(id);
        if (vmUserSettings == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(vmUserSettings);
    }


}
